package swf;

class Rectangle extends BitStream {
	public static function fromTwips ( x : Int, y : Int, width : Int, height : Int ) : Rectangle {
		var maxBits = Math.round( Math.max( Math.max(findBitLength( x ), findBitLength( y )), Math.max(findBitLength( width ), findBitLength( height )) ) );
		var rect = new Rectangle();
		rect.appendInt( maxBits, 5 );
		rect.appendInt( x, maxBits, true );
		rect.appendInt( x + width, maxBits, true );
		rect.appendInt( y, maxBits, true );
		rect.appendInt( y + height, maxBits, true );
		
		return rect;
	}
	
	static function findBitLength ( n : Int ) : Int {
		var abs = Math.abs( n );
		var i = 0;
		while( Math.pow( 2, i++ ) < abs ) {}
		
		// add one bit for the sign
		return 1 + i;
	}
	
	var bits : Int;
	
	private function new () {
		super();
	}
}
